package com.pactera.miyuangroup.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * MD5
 *
 * @author Wangwu
 * @create 2018-04-17 下午4:20
 **/
public class MD5Utils {

    private static final Logger log = LoggerFactory.getLogger(MD5Utils.class);

    public MD5Utils() {
    }

    public static String encode(String text) {
        try {
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] buffer = digest.digest(text.getBytes());
            StringBuffer sb = new StringBuffer();
            byte[] var4 = buffer;
            int var5 = buffer.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                byte b = var4[var6];
                int a = b & 255;
                String hex = Integer.toHexString(a);
                if (hex.length() == 1) {
                    hex = 0 + hex;
                }

                sb.append(hex);
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException var10) {
            log.error("IOException:{}", var10.getMessage());
            return null;
        }
    }

    public static String encode(InputStream in) {
        try {
            MessageDigest digester = MessageDigest.getInstance("MD5");
            byte[] bytes = new byte[8192];

            int byteCount;
            while((byteCount = in.read(bytes)) > 0) {
                digester.update(bytes, 0, byteCount);
            }

            byte[] digest = digester.digest();
            StringBuffer sb = new StringBuffer();
            byte[] var6 = digest;
            int var7 = digest.length;

            for(int var8 = 0; var8 < var7; ++var8) {
                byte b = var6[var8];
                int a = b & 255;
                String hex = Integer.toHexString(a);
                if (hex.length() == 1) {
                    hex = 0 + hex;
                }

                sb.append(hex);
            }

            String var22 = sb.toString();
            return var22;
        } catch (Exception var20) {
            log.error("Exception:{}", var20.getMessage());
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException var19) {
                    log.error("IOException:{}", var19.getMessage());
                }

                in = null;
            }

        }

        return null;
    }

    public static String sign(String text, String key) {
        text = text + key;
        return DigestUtils.md5Hex(getContentBytes(text));
    }

    public static boolean verify(String text, String sign, String key) {
        text = text + key;
        String mysign = DigestUtils.md5Hex(getContentBytes(text));
        return mysign.equals(sign);
    }

    public static String createLinkString(Map<String, String> params) {
        List<String> keys = new ArrayList(params.keySet());
        Collections.sort(keys);
        String prestr = "";

        for(int i = 0; i < keys.size(); ++i) {
            String key = (String)keys.get(i);
            String value = EncodeUtils.urlEncode((String)params.get(key));
            if (i == keys.size() - 1) {
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + key + "=" + value + "&";
            }
        }

        return prestr;
    }

    private static byte[] getContentBytes(String content) {
        try {
            return content.getBytes("UTF-8");
        } catch (UnsupportedEncodingException var2) {
            log.error("MD5签名过程中出现错误,指定的编码集不对(UnsupportedEncodingException):{}", var2.getMessage());
            return new byte[0];
        }
    }
}
